package cn.iocoder.yudao.module.system.controller.admin.lottery;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.controller.admin.lottery.vo.LotteryConfigRespVO;
import cn.iocoder.yudao.module.system.controller.admin.lottery.vo.LotteryConfigSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.lottery.LotteryConfigDO;
import cn.iocoder.yudao.module.system.service.lottery.LotteryConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 抽奖配置 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 抽奖配置")
@RestController
@RequestMapping("/system/lottery-config")
@Validated
@Slf4j
public class LotteryConfigController {

    @Resource
    private LotteryConfigService lotteryConfigService;

    @GetMapping("/get")
    @Operation(summary = "根据配置键获取配置")
    @Parameter(name = "configKey", description = "配置键", required = true, example = "daily_lottery_limit")
    @PermitAll
    public CommonResult<LotteryConfigRespVO> getConfig(@RequestParam("configKey") String configKey) {
        LotteryConfigDO config = lotteryConfigService.getConfigByKey(configKey);
        if (config == null) {
            return success(null);
        }
        
        LotteryConfigRespVO respVO = new LotteryConfigRespVO();
        respVO.setId(config.getId());
        respVO.setConfigKey(config.getConfigKey());
        respVO.setConfigValue(config.getConfigValue());
        respVO.setConfigName(config.getConfigName());
        respVO.setConfigDesc(config.getConfigDesc());
        respVO.setEnabled(config.getEnabled());
        respVO.setCreateTime(config.getCreateTime());
        respVO.setUpdateTime(config.getUpdateTime());
        
        return success(respVO);
    }

    @PostMapping("/save")
    @Operation(summary = "保存抽奖配置")
    @PermitAll
    public CommonResult<Boolean> saveConfig(@Valid @RequestBody LotteryConfigSaveReqVO reqVO) {
        lotteryConfigService.saveOrUpdateConfig(
                reqVO.getConfigKey(), 
                reqVO.getConfigValue(), 
                reqVO.getConfigName(), 
                reqVO.getConfigDesc()
        );
        return success(true);
    }

    @PostMapping("/init")
    @Operation(summary = "初始化默认配置")
    @PermitAll
    public CommonResult<Boolean> initDefaultConfigs() {
        lotteryConfigService.initDefaultConfigs();
        return success(true);
    }

    @GetMapping("/probability/list")
    @Operation(summary = "获取所有概率配置")
    @PermitAll
    public CommonResult<List<LotteryConfigRespVO>> getAllProbabilityConfigs() {
        // 定义所有概率配置的键
        List<String> probabilityKeys = Arrays.asList(
                "full_package_prob_with_stock",
                "professional_report_prob_with_stock",
                "basic_report_prob_with_stock",
                "ask_count_prob_with_stock",
                "content_member_prob_with_stock",
                "no_prize_prob_with_stock",
                "full_package_prob_no_stock",
                "basic_report_prob_no_stock",
                "ask_count_prob_no_stock",
                "content_member_prob_no_stock",
                "no_prize_prob_no_stock"
        );

        List<LotteryConfigRespVO> configs = probabilityKeys.stream()
                .map(key -> {
                    LotteryConfigDO config = lotteryConfigService.getConfigByKey(key);
                    if (config != null) {
                        LotteryConfigRespVO respVO = new LotteryConfigRespVO();
                        respVO.setId(config.getId());
                        respVO.setConfigKey(config.getConfigKey());
                        respVO.setConfigValue(config.getConfigValue());
                        respVO.setConfigName(config.getConfigName());
                        respVO.setConfigDesc(config.getConfigDesc());
                        respVO.setEnabled(config.getEnabled());
                        respVO.setCreateTime(config.getCreateTime());
                        respVO.setUpdateTime(config.getUpdateTime());
                        return respVO;
                    }
                    return null;
                })
                .filter(config -> config != null)
                .collect(Collectors.toList());

        return success(configs);
    }
}
